package com.primeton.esper.main;

import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.UpdateListener;
import com.primeton.esper.config.ConfigurationFactory;
import com.primeton.esper.evententry.PersonEvent;

public class BatchTest {

	private static EPServiceProvider	epService	= EPServiceProviderManager.getDefaultProvider(ConfigurationFactory
																									.getEsperConfiguration());

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		testBatch();
	}

	public static void testBatch() {
		String express = "select distinct name as pName,avg(age) as avgA from PersonEvent.win:length_batch(3)";
		EPStatement statement = epService.getEPAdministrator().createEPL(express);
		statement.addListener(new UpdateListener() {

			@Override
			public void update(EventBean[] newEvents, EventBean[] oldEvents) {
				// TODO Auto-generated method stub
				System.out.println(newEvents);
				for (EventBean eventBean : newEvents) {
					System.out.println(eventBean.get("pName"));
					System.out.println(eventBean.get("avgA"));
				}
				System.out.println(newEvents.length);
				EventBean event = newEvents[0];
				// System.out.println(event.get("avg(age)"));

			}
		});
		PersonEvent p1 = new PersonEvent("1", "songfei", 21);
		PersonEvent p2 = new PersonEvent("2", "zhang", 22);
		PersonEvent p3 = new PersonEvent("3", "zhang", 20);
		PersonEvent p4 = new PersonEvent("4", "zhang", 20);
		PersonEvent p5 = new PersonEvent("2", "zhang", 23);
		PersonEvent p6 = new PersonEvent("2", "zhang", 26);
		epService.getEPRuntime().sendEvent(p1);
		epService.getEPRuntime().sendEvent(p2);
		epService.getEPRuntime().sendEvent(p3);
		epService.getEPRuntime().sendEvent(p4);
		epService.getEPRuntime().sendEvent(p5);
		epService.getEPRuntime().sendEvent(p6);
	}
}
